package com.reverie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reverie.common.pojo.CommonPageReq;
import com.reverie.common.pojo.CommonResult;
import com.reverie.dto.common.DeleteParam;
import com.reverie.entity.CategoryRelGood;
import com.reverie.entity.GoodCategory;
import com.reverie.entity.Goods;
import com.reverie.service.ICategoryRelGoodService;
import com.reverie.service.IGoodCategoryService;
import com.reverie.service.IGoodsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("goodCategory")
public class GoodCategoryController {

    @Resource
    private IGoodCategoryService goodCategoryService;
    @Resource
    private ICategoryRelGoodService categoryRelGoodService;
    @Resource
    private IGoodsService goodsService;

    @PostMapping("queryPage")
    public CommonResult<Page<GoodCategory>> queryPage(@RequestBody CommonPageReq<GoodCategory> pageReq) {
        return CommonResult.data(goodCategoryService.queryPage(pageReq));
    }

    @PostMapping("save")
    public CommonResult<Boolean> saveGoodCategory(@RequestBody GoodCategory goodCategory) {
        return CommonResult.data(goodCategoryService.saveOrUpdate(goodCategory));
    }

    @PostMapping("saveGoods")
    public CommonResult<Boolean> saveGoods(@RequestBody GoodCategory goodCategory) {
        return CommonResult.data(goodCategoryService.saveGoods(goodCategory));
    }

    @PostMapping("queryGoods")
    public CommonResult<List<CategoryRelGood>> queryGoods(@RequestBody GoodCategory goodCategory) {
        List<CategoryRelGood> categoryRelGoods = categoryRelGoodService.queryGoods(goodCategory.getId());
        if(CollectionUtils.isEmpty(categoryRelGoods)){
            return CommonResult.data(new ArrayList<>());
        }
        Set<Integer> goodIds = new HashSet<>();
        categoryRelGoods.forEach(categoryRelGood -> {
            goodIds.add(categoryRelGood.getGoodId());
        });
        List<Goods> goods = goodsService.listByIds(goodIds);
        Map<Integer,Goods> goodsMap = goods.stream().collect(Collectors.toMap(Goods::getGoodsId,g -> g));
        categoryRelGoods.forEach(categoryRelGood -> {
            categoryRelGood.setGood(goodsMap.getOrDefault(categoryRelGood.getGoodId(),null));
        });
        return CommonResult.data(categoryRelGoods.stream().filter(good -> good.getGood() != null).sorted(Comparator.comparing(CategoryRelGood::getSortId)).collect(Collectors.toList()));
    }

    @PostMapping("deleteCategoryRelGood")
    public CommonResult<Boolean> deleteCategoryRelGood(@RequestBody DeleteParam deleteParam) {
        return CommonResult.data(categoryRelGoodService.removeById(deleteParam.getId()));
    }

    @PostMapping("switchPublish")
    public CommonResult<Boolean> switchPublish(@RequestBody GoodCategory goodCategory) {
        GoodCategory gc = goodCategoryService.getById(goodCategory.getId());
        gc.setPublished(!gc.getPublished());
        return CommonResult.data(goodCategoryService.updateById(gc));
    }


    @PostMapping("delete")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> delete(@RequestBody DeleteParam deleteParam) {
        goodCategoryService.removeById(deleteParam.getId());
        categoryRelGoodService.removeByCategoryId(deleteParam.getId());
        return CommonResult.data(true);
    }

}
